package diamondRingServlet;

import diamondRingEntity.Admin;
import diamondRingEntity.DiamondRingEntity;
import diamondRingService.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/BackInsertGoodsServlet")
public class BackInsertGoodsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        AdminServiceImpl adminService=new AdminServiceImpl();

        int count = 0;
        int goods = 1;
        System.out.println(goods);

        String path1 = request.getParameter("goodspath1");
        String path2 = request.getParameter("goodspath2");

        String newPath1 = path1.substring(12,path1.length());
        String newPath2 = path2.substring(12,path2.length());

        String newPathOne = "images/"+newPath1;
        System.out.println(newPathOne);
        String newPathTow = "images/"+newPath2;
        System.out.println(newPathTow);


        String series = request.getParameter("series");
        System.out.println(series);
        String style = request.getParameter("style");
        System.out.println(style);
        String texture = request.getParameter("texture");
        System.out.println(texture);
        String modeid = request.getParameter("modeid");
        System.out.println(modeid);
        String shape = request.getParameter("shape");
        System.out.println(shape);
        String object = request.getParameter("object");
        System.out.println(object);
        String type = request.getParameter("type");
        System.out.println(type);
        String colour = request.getParameter("colour");
        System.out.println(colour);
        String carat = request.getParameter("carat");
        System.out.println(carat);
        int diamondprice = Integer.parseInt(request.getParameter("diamondprice"));
        System.out.println(diamondprice);
        int diamondnum = Integer.parseInt(request.getParameter("diamondnum"));
        System.out.println(diamondnum);
        String caratweight = request.getParameter("caratweight");
        System.out.println(caratweight);
        int stock = Integer.parseInt(request.getParameter("stock"));
        System.out.println(stock);

        DiamondRingEntity diamondRingEntity = new DiamondRingEntity(newPathOne,newPathTow,series,style,texture,modeid,shape,object,type,colour,carat,diamondprice,diamondnum,caratweight,stock,goods);

        if (adminService.Insert(diamondRingEntity)>0){
            count = 1;
        }else{
            count = -1;
        }
        out.print(count);
        out.flush();
        out.close();


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}